package com.gxuwz.restaurant.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxuwz.restaurant.common.Result;
import com.gxuwz.restaurant.entity.*;
import com.gxuwz.restaurant.mapper.CartMapper;
import com.gxuwz.restaurant.mapper.GoodsMapper;
import com.gxuwz.restaurant.mapper.SeatManageMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;


@RestController
@RequestMapping("/order")
public class OrderController {
    @Resource
    SeatManageMapper seatManageMapper;
    @Resource
    CartMapper cartMapper;
    @Resource
    GoodsMapper goodsMapper;



    @GetMapping("/selectSeat")   //get用来查询
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search,
                              @RequestParam(defaultValue = "") String cleanstate,
                              @RequestParam(defaultValue = "") String seatState
                              ) {

        LambdaQueryWrapper<SeatManage> wrapper = Wrappers.<SeatManage>lambdaQuery();


        if (StrUtil.isNotBlank(search)) {
            wrapper.like(SeatManage::getSeatNumber, search);
        }
        if (StrUtil.isNotBlank(seatState)) {
            wrapper.eq(SeatManage::getSeatState,seatState);
        }
        if (StrUtil.isNotBlank(cleanstate)) {
            wrapper.eq(SeatManage::getCleanstate,cleanstate);
        }





        Page<SeatManage> Page = seatManageMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);


        return Result.success(Page);
    }

    @PostMapping("/addseat")//post用来新增

    public Result<?> addseat(@RequestBody SeatManage seatManage){
        System.out.println("执行座位添加");

        System.out.println(seatManage);
        seatManage.setSeatState("true");
        seatManage.setCleanstate("打扫完成");
        int insert = seatManageMapper.insert(seatManage);
        System.out.println(insert);



        return Result.success();
    }

    @PutMapping("/updateSeat") //put用来更新
    public Result<?> updateseat(@RequestBody SeatManage seatManage){
        System.out.println("执行编辑座位信息方法+++");


            seatManageMapper.updateById(seatManage);
            return Result.success();
        }
    @GetMapping("/{id}")   //检查座位状态
    public Result<?> showMenu(@PathVariable long id) {

        QueryWrapper<SeatManage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        SeatManage menus = seatManageMapper.selectOne(queryWrapper);

        return Result.success(menus);
    }


    @PostMapping("/buy")

    public Result<?> buy(@RequestParam String money, @RequestBody List<Cart> cart, @RequestParam String seatid) {

        System.out.println("执行购买");
        String orderNo = IdUtil.getSnowflake().nextIdStr();



        System.out.println(cart);
        for (Cart cart1 : cart) {

            Goods goods = goodsMapper.selectById(cart1.getId());
            int stock =goods.getStock();
                goods.setStock(stock-1);
                goodsMapper.updateById(goods);


            cart1.setPayState("未支付");
            cart1.setOrderNo(orderNo);
            String randomId = IdUtil.getSnowflake().nextIdStr();
            cart1.setId(randomId);
            cartMapper.insert(cart1);

        }
        String payUrl = "http://localhost:9480/alipay/pay?subject=" + seatid + "&traceNo=" + orderNo + "&totalAmount=" + money ;
        return Result.success(payUrl);
    }
    @DeleteMapping("/deleteSeat/{id}") //DELETE用来删除
    public Result<?> deleteSeat(@PathVariable long id){
        System.out.println("执行删除座位方法");
        seatManageMapper.deleteById(id);

        return Result.success();


    }
    @PutMapping("/cleanSeat/{id}")//put用来更新

    public Result<?> cleanSeat(@PathVariable int id) {
        SeatManage seatManage = new SeatManage();
        seatManage.setId(id);
        seatManage.setCleanstate("需要打扫");
        seatManageMapper.updateById(seatManage);
        return Result.success();
    }

    @PutMapping("/releaseSeat/{id}")//put用来更新

    public Result<?> releaseSeat(@PathVariable int id) {
        System.out.println(id);
        SeatManage seatManage = new SeatManage();
        seatManage.setId(id);
        seatManage.setSeatState("false");
        seatManage.setCleanstate("需要打扫");
        seatManage.setOrderNo("暂无数据");
        seatManageMapper.updateById(seatManage);
        return Result.success();
    }

    @GetMapping("/loadOrder")   //get用来查询

    public Result<?> findPageorder(@RequestParam(defaultValue = "1") Integer pageNum,
                                   @RequestParam(defaultValue = "10") Integer pageSize,
                                   @RequestParam(defaultValue = "") String search) {

        LambdaQueryWrapper<Cart> wrapper = Wrappers.<Cart>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Cart::getOrderNo, search);
        }
          wrapper.eq(Cart::getGoodsState,"正在制作");

        Page<Cart> orderPage = cartMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(orderPage);


    }


   @GetMapping("loadOrder/Unpay")   //get用来查询
    public Result<?> findPageorderUnpay(@RequestParam(defaultValue = "1") Integer pageNum,
                                                @RequestParam(defaultValue = "10") Integer pageSize,
                                                @RequestParam(defaultValue = "") String search,
                                                @RequestParam(defaultValue = "") String seatNumber) {
        LambdaQueryWrapper<Cart> wrapper = Wrappers.<Cart>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Cart::getOrderNo, search);
        }else if(StrUtil.isNotBlank(seatNumber)){
            wrapper.like(Cart::getSeatNumber, seatNumber);
        }

        wrapper.eq(Cart::getPayState,"未支付");


        Page<Cart> orderPage = cartMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(orderPage);


    }
    @GetMapping("loadOrder/history")   //get用来查询
    public Result<?> findPageorderHistory(@RequestParam(defaultValue = "1") Integer pageNum,
                                        @RequestParam(defaultValue = "10") Integer pageSize,
                                        @RequestParam(defaultValue = "") String search,
                                          @RequestParam(defaultValue = "") String seatNumber) {
        LambdaQueryWrapper<Cart> wrapper = Wrappers.<Cart>lambdaQuery();
        System.out.println(seatNumber);
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Cart::getOrderNo, search);
        }else if(StrUtil.isNotBlank(seatNumber)){
            wrapper.eq(Cart::getSeatNumber, seatNumber);
        }
        wrapper.eq(Cart::getPayState,"已支付");
        wrapper.eq(Cart::getGoodsState,"已出餐");

        Page<Cart> orderPage = cartMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        System.out.println(orderPage.getRecords());
        return Result.success(orderPage);


    }

    @GetMapping("/loadOrder/RunningOrder")   //get用来查询
    public Result<?> findPageorderRunningOrder(@RequestParam(defaultValue = "1") Integer pageNum,
                                          @RequestParam(defaultValue = "10") Integer pageSize,
                                          @RequestParam(defaultValue = "") String search,
                                          @RequestParam(defaultValue = "") String seatNumber) {
        LambdaQueryWrapper<Cart> wrapper = Wrappers.<Cart>lambdaQuery();
        System.out.println(seatNumber);
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Cart::getOrderNo, search);
        }else if(StrUtil.isNotBlank(seatNumber)){
            wrapper.eq(Cart::getSeatNumber, seatNumber);
        }
        wrapper.eq(Cart::getPayState,"已支付");
        wrapper.eq(Cart::getGoodsState,"正在制作");

        Page<Cart> orderPage = cartMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(orderPage);


    }

    @GetMapping("loadOrder/history_today")   //get用来查询
    public Result<?> findPageorderHistory_today(@RequestParam(defaultValue = "1") Integer pageNum,
                                          @RequestParam(defaultValue = "10") Integer pageSize,
                                          @RequestParam(defaultValue = "") String search,
                                          @RequestParam(defaultValue = "") String seatNumber) {
        LambdaQueryWrapper<Cart> wrapper = Wrappers.<Cart>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Cart::getOrderNo, search);
        }else if(StrUtil.isNotBlank(seatNumber)){
            wrapper.like(Cart::getSeatNumber, seatNumber);
        }
        Date today =new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        wrapper.like(Cart::getCreatTime,sdf.format(today));
        wrapper.eq(Cart::getPayState,"已支付");
        wrapper.eq(Cart::getGoodsState,"已出餐");

        Page<Cart> orderPage = cartMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(orderPage);


    }


    //==================
    @GetMapping("loadOrder/history_seat")   //get用来查询
    public Result<?> findPageorderHistory_seat() {

        LambdaQueryWrapper<Cart> wrapper = Wrappers.<Cart>lambdaQuery();
        wrapper.eq(Cart::getPayState,"已支付");
        wrapper.eq(Cart::getGoodsState,"已出餐");

        Page<Cart> orderPage = cartMapper.selectPage(new Page<>(), wrapper);
        Set hs = new HashSet();

               for(Cart cart: orderPage.getRecords()){
                   hs.add(cart.getSeatNumber());
               }
        return Result.success(hs);


    }

    @GetMapping("loadOrder/seat_Running")   //get用来查询
    public Result<?> findPageorderseat_Running() {

        LambdaQueryWrapper<Cart> wrapper = Wrappers.<Cart>lambdaQuery();
        wrapper.eq(Cart::getPayState,"已支付");
        wrapper.eq(Cart::getGoodsState,"正在制作");

        Page<Cart> orderPage = cartMapper.selectPage(new Page<>(), wrapper);
        Set hs = new HashSet();

        for(Cart cart: orderPage.getRecords()){
            hs.add(cart.getSeatNumber());
        }
        return Result.success(hs);


    }
    @GetMapping("/loadOrder/Unpay_seat")   //get用来查询
    public Result<?> findPageorderUnpay_seat() {

        LambdaQueryWrapper<Cart> wrapper = Wrappers.<Cart>lambdaQuery();
        wrapper.eq(Cart::getPayState,"未支付");


        Page<Cart> orderPage = cartMapper.selectPage(new Page<>(), wrapper);
        Set hs = new HashSet();

        for(Cart cart: orderPage.getRecords()){
            hs.add(cart.getSeatNumber());
        }
        return Result.success(hs);


    }

    @GetMapping("loadOrder/history_seat_today")   //get用来查询
    public Result<?> findPageorderHistory_seat_todya() {

        LambdaQueryWrapper<Cart> wrapper = Wrappers.<Cart>lambdaQuery();
        wrapper.eq(Cart::getPayState,"已支付");
        wrapper.eq(Cart::getGoodsState,"已出餐");
        Date today =new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        wrapper.like(Cart::getCreatTime,sdf.format(today));

        Page<Cart> orderPage = cartMapper.selectPage(new Page<>(), wrapper);
        Set hs = new HashSet();

        for(Cart cart: orderPage.getRecords()){
            hs.add(cart.getSeatNumber());
        }
        return Result.success(hs);


    }





    @GetMapping("/ProcessingOrder")   //get用来查询
    public Result<?> ProcessingOrder(
            @RequestParam(defaultValue = "") String seatNumber

    ) {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        if(StrUtil.isNotBlank(seatNumber)){
            queryWrapper.eq("seat_number", seatNumber);
        }
        queryWrapper.eq("goods_state", "正在制作");
        queryWrapper.eq("pay_state", "已支付");
        queryWrapper.eq("category", "1");
        List<Cart> carts = cartMapper.selectList(queryWrapper);
        return Result.success(carts);


    }
    @GetMapping("/ProcessingOrder_seat")   //get用来查询
    public Result<?> findProcessingOrder_seat(

    ) {
        LambdaQueryWrapper<Cart> wrapper = Wrappers.<Cart>lambdaQuery();
        wrapper.eq(Cart::getPayState, "已支付");
        wrapper.eq(Cart::getGoodsState, "正在制作");
        wrapper.eq(Cart::getCategory, "1");

        Page<Cart> orderPage = cartMapper.selectPage(new Page<>(), wrapper);
        Set hs = new HashSet();

        for (Cart cart : orderPage.getRecords()) {
            hs.add(cart.getSeatNumber());
        }
        System.out.println(hs);
        return Result.success(hs);
    }

    @PutMapping ("/finishOrder")//put用来更新
    public Result<?> update(@RequestBody Cart cart){
        System.out.println(cart);
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", cart.getId());
//        queryWrapper.eq("order_no", cart.getOrderNo());
        Cart cart1 =new Cart();
        cart1.setGoodsState("已出餐");
       cartMapper.update(cart1, queryWrapper);


        return Result.success();


    }

    @PutMapping ("/cancelOrder/{id}")//put用来更新
    public Result<?> cancelOrder(@PathVariable String id){
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
       queryWrapper.eq("id",id);
        int delete = cartMapper.delete(queryWrapper);
        if (delete>0){
           return Result.success();
       }else {
           return Result.error("1","取消失败");
       }






    }


}








